import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat_ui/utils/x_load_svg.dart';
import 'package:flutter_chat_ui/utils/utils.dart';

///
/// 弹窗
///
class DialogUtils {
  ///
  /// 跳转系统权限设置界面
  ///
  static Future<void> showAppSettingsDialog(
    BuildContext context, {
    required String title,
    required String message,
  }) async {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
            content: Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Text(
                message,
                textAlign: TextAlign.center,
                style: const TextStyle(fontSize: 15),
              ),
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text(
                  '取消',
                  style: TextStyle(color: Colors.black),
                ),
              ),
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                  Future.delayed(const Duration(milliseconds: 255), () => Utils.xOpenAppSettings());
                },
                child: const Text(
                  '确定',
                  style: TextStyle(color: Color(0xff3388FF)),
                ),
              ),
            ],
          );
        });
  }

  ///
  /// 删除会话的弹窗
  ///
  static void showDeleteSessionDialog(
    BuildContext context, {
    VoidCallback? onDelete,
  }) {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                XSvgImage.load(
                  'icon_warning',
                  width: 20,
                  height: 20,
                ),
                const Padding(
                  padding: EdgeInsets.only(left: 8),
                  child: Text(
                    '确定删除对话？',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ],
            ),
            content: const Padding(
              padding: EdgeInsets.only(top: 8.0),
              child: Text(
                '删除后，聊天记录将不可恢复。',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 15),
              ),
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text(
                  '取消',
                  style: TextStyle(color: Colors.black),
                ),
              ),
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                  Future.delayed(const Duration(milliseconds: 300), () => onDelete?.call());
                },
                style: ButtonStyle(
                  backgroundColor: WidgetStateProperty.all(Colors.red),
                  shape: WidgetStateProperty.all(
                    RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
                  ),
                ),
                child: const Text(
                  '删除',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ],
          );
        });
  }

  ///
  /// 确认修改会话名弹窗
  ///
  static void showEditSessionNameDialog(
    BuildContext context, {
    required String defaultName,
    ValueChanged<String>? onRename,
  }) {
    String changeText = defaultName.trim();

    // 显示编辑框
    showCupertinoDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return CupertinoAlertDialog(
            title: const Text(
              '编辑对话名称',
              textAlign: TextAlign.start,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            content: Material(
              color: Colors.transparent,
              child: Container(
                margin: const EdgeInsets.only(top: 8),
                child: Column(
                  children: [
                    /// 文本输入框
                    TextField(
                      controller: TextEditingController(text: defaultName),
                      onChanged: (value) {
                        changeText = value;
                      },
                      cursorColor: const Color(0xff155AEF),
                      autofocus: true,
                      style: const TextStyle(
                        fontSize: 16,
                        color: Colors.black,
                        fontWeight: FontWeight.w500,
                      ),
                      decoration: const InputDecoration(
                        contentPadding: EdgeInsets.only(left: 8),
                        // 自定义下划线样式
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Color(0xff155AEF)),
                        ),
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Color(0xff155AEF)),
                        ),
                        hintText: '输入名称',
                        hintStyle: TextStyle(
                          fontSize: 14,
                          color: Color(0xffababab),
                          fontWeight: FontWeight.w300,
                        ),
                      ),
                    ),

                    /// 取消、确定按钮
                    Container(
                      margin: const EdgeInsets.only(top: 20),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          /// 取消
                          TextButton(
                            onPressed: () => Navigator.pop(context),
                            style: ButtonStyle(
                              shape: WidgetStateProperty.all(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(8),
                                  side: BorderSide(
                                    color: Colors.grey.withValues(alpha: 0.25),
                                  ),
                                ),
                              ),
                            ),
                            child: const Text(
                              '取消',
                              style: TextStyle(color: Colors.black),
                            ),
                          ),

                          /// 间距
                          const SizedBox(width: 15),

                          /// 确定
                          TextButton(
                            onPressed: () {
                              Navigator.pop(context);
                              // 输入框内容为空或者与默认值相同，则不做任何操作。
                              if (defaultName.trim() == changeText.trim() && Utils.isBlank(changeText.trim())) {
                                return;
                              }
                              Future.delayed(const Duration(milliseconds: 300), () => onRename?.call(changeText.trim()));
                            },
                            style: ButtonStyle(
                              shape: WidgetStateProperty.all(
                                RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(8),
                                  side: const BorderSide(
                                    color: Color(0xff155AEF),
                                  ),
                                ),
                              ),
                            ),
                            child: const Text(
                              '确定',
                              style: TextStyle(color: Color(0xff155AEF)),
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          );
        });
  }
}
